1. /* sdostrm.cpp by K.Tsuru */
  2. // function ID 356 DRADIX since version 2.21
  3. /*****************************************************************
  4. SDouble class
  5. It provides the output stream.
  6. usage : cout << x;
  7. output format : see SetFormat()
  8. "stringstream" is partialy used.
  9. Mainly "sprintf()" function.
  10. ******************************************************************/
  11. #ifndef SN_H
  12. #include "sn.h"
  13. #endif
  14. // static objects of SDouble class
  15. long SDouble::figUnit = 10L;
  16. long SDouble::putFig = 0;
  17. int SDouble::perLine = 0;
  18. int SDouble::lineFormat = SDouble::CRLF|SDouble::ROUND|SDouble::INT_PUT;
  19. int SDouble::delmt = ' ';
  20. long SDouble::ioCount = 0; // the number of input or output characters
  21. long SDouble::crCount = 0;
  22. // static void SetFormat(long fU, long pF = 0, int pL = 0, int lF = ROUND|INT_PUT|CRLF, int dm = ' '); in sdbl.h
  23. void SDouble::SetFormat(long fU, long pF, int pL, int lF, int dm){
  24. figUnit = fU; putFig = pF; perLine = pL; lineFormat = lF;
  25. delmt = dm;
  26. } // 359
  27. void SDouble::DefaultFormat() { // It restores the default values. 360
  28. SetFormat(10);
  29. }
  30. //-------------------------------------------------------------------------------
  31. static int putcr(int cr, ostream& os){
  32. if(cr) { os << endl; SDouble::crCount++; }
  33. return cr ? 1 : 0;
  34. }
  35. static int putdelmt(char d, ostream& os){
  36. if(d) os << d;
  37. return d ? 1 : 0;
  38. }
  39. static int putbuff(const char* buff, ostream& os) {
  40. int len = strlen(buff);
  41. os.write(buff, len);
  42. return len;
  43. }
  44. // function ID = 358
  45. /*********
  46. main body
  47. **********/
  48. ostream& operator<<(ostream& os, const SDouble& sd) {
  49. sd.crCount = 0;
  50. long fig = sd.figUnit, pF = sd.putFig;
  51. int pL = sd.perLine, fmt =sd.lineFormat, dm = sd.delmt;
  52. sd.SignCheck(358); // sign == UNDECIDED ?
  53. // FILE* out = FileStream();
  54. if(!fig){ fig = LONG_MAX; dm = 0; } // continuously output with no delimiter
  55. int showFig = fmt & sd.SHOW_FIG;
  56. int crlf = fmt & sd.END_CR;
  57. if( sd.Sign(344) == 0 ){
  58. sd.ioCount = putbuff("0.0", os); sd.ioCount += putcr(crlf, os); return os;
  59. // fprintf(out, "0.0"); return 3+putcr(crlf, out);
  60. }
  61. SDouble temp(sd);
  62. long cCount = 0;
  63. int nc = 0; //the number of characters in a line
  64. long decCount = 0; //0.abcd|efgh|.... counter of decimal part
  65. temp.StdReform(344); //standard form
  66. // lineFormat temp display initial value of decCount
  67. // lineFormat & INT_PUT 0|000a|bcde|fgh..... a.bcdefgh... DFIGURES
  68. // else 0|abcd|efgh|..... 0.abcdefgh.... 0
  69. uint last; //last position of output
  70. if(sd.lineFormat & sd.ROUND) temp.Round(); //round off
  71. long exp = DFIGURES*(long)temp.RdxExp();//exponent in a decimal system
  72. ulong v = (ulong)temp.figure(1);
  73. #ifndef NDEBUG
  74. assert(v);
  75. #endif
  76. //It reforms into "0.abcd ...".
  77. int k = 0;
  78. while(v < DRADIX){ v *= 10; k++; }
  79. k--;
  80. temp.TempPointFree(); //temporally into floating point mode
  81. if(k > 0) {
  82. v = (ulong)ipow10(k); temp = DsMult(temp, v); exp -= k;
  83. }
  84. if(sd.lineFormat & sd.INT_PUT){
  85. //In order to display such as "a.bcdef...." it multiplies by ten.
  86. if(pF > 1) pF--;
  87. temp = DsMult(temp, 10); exp--;
  88. decCount = DFIGURES;
  89. }
  90. temp.PointModePop();
  91. if(sd.lineFormat & sd.ROUND) last = min(temp.Last(), temp.EffFig()); //do not display hidden figures
  92. else last = temp.Last();
  93. //the number of decimal figures between decCount/DFIGURES and last
  94. long decfig = long(last - decCount/DFIGURES)*(long)DFIGURES; //maybe last=1
  95. if(pF > 0) decfig = min(decfig, pF);
  96. //display the negative sign
  97. if( temp.Sign(344) == -1 ){ os.put('-'); cCount++; }
  98. //display the integral part
  99. char buff[displayWidth+1];
  100. string sbuff("0");
  101. stringstream ss;
  102. //temp.Puts();
  103. // buff[0] = '0'; buff[1] = '\0'; // buff[] = "0"
  104. if(decCount){
  105. ss << temp.figure(1);
  106. sbuff= ss.str(); //buff[0] += temp.figure(1);
  107. // cout << sbuff << endl;
  108. }
  109. if( (decfig + decfig/fig) > (int)displayWidth ){
  110. sbuff += ".+"; //strcat(buff, ".+");
  111. if(sd.lineFormat & sd.CONTINUE) sbuff += "\\"; // strcat(buff, "\\");
  112. cCount += sbuff.length(); os << sbuff; cCount += putcr(1, os); // fprintf(out, "%s\n",buff); //over one line
  113. } else {
  114. sbuff += "."; //strcat(buff, ".");
  115. cCount += sbuff.length(); os << sbuff;// fprintf(out, "%s.",buff); //show point
  116. nc = (int)cCount;
  117. }
  118. //decimal part
  119. int rest; //rest of characters at last line
  120. int figWidth; //the width of figure display at the end of line, except ()
  121. int dec_init = (int)decCount;
  122. if(!decfig){ //decimal part=0
  123. os.put('0'); cCount++;
  124. rest = displayWidth - (int)cCount;
  125. } else {
  126. //non zero decimal part
  127. int putdec = 0, crLine, figCount = 0;
  128. figWidth = iFigures(decfig);
  129. crLine = (pL > 0 ) ? pL : int( (displayWidth-1)/(fig+1) );
  130. if(showFig) while((int)displayWidth -crLine*(fig+1)<(figWidth+3) ) crLine--;
  131. if( crLine<=0 ) crLine = 1;
  132. temp.CharNthDec(-1L);
  133. while(decfig){
  134. os.put( temp.CharNthDec(decCount) );
  135. decfig--; cCount++; decCount++; nc++; putdec++;
  136. if(putdec == fig){
  137. figCount++; nc++; putdec = 0;
  138. if( figCount != crLine ) cCount += putdelmt(dm, os);
  139. }
  140. if( figCount == crLine ){ //reaches at line end
  141. if(showFig) {
  142. #if 0 // sprintf() version (very simple)
  143. cCount += sprintf(buff, " (%*ld)", figWidth, decCount - dec_init);
  144. os << buff;
  145. #else // "stringstream" version (too complicated for me)
  146. ss.str(""); // clear buffer
  147. ss.clear(stringstream::goodbit);
  148. ss << " (";
  149. ss.width(figWidth); ss.fill(' '); ss << decCount - dec_init;
  150. ss << ')';
  151. os << ss.str(); cCount += strlen(ss.str().c_str());
  152. #endif
  153. }
  154. if(dm != ' ') cCount += putdelmt(dm, os);
  155. if(sd.lineFormat & sd.CONTINUE){ os.put('\\'); cCount++; }
  156. if(sd.lineFormat & (sd.CRLF|sd.SHOW_FIG)){
  157. putcr(1, os); cCount++;
  158. }
  159. figCount = 0; nc = 0;
  160. }
  161. }
  162. rest = int((fig+1)*crLine -nc);
  163. }
  164. //display exponent part
  165. //"sprintf()" version only below.
  166. if(exp){
  167. cCount += sprintf(buff,"e%+ld",exp);
  168. if(rest < (int)strlen(buff) ){
  169. if(sd.lineFormat & sd.CONTINUE){ os.put('\\'); cCount++; }
  170. putcr(1, os); cCount++; rest = displayWidth;
  171. }
  172. rest -= putbuff(buff, os); //fprintf(out, "%s", buff);
  173. }
  174. //display the number of figures at the last line
  175. if(showFig && nc && (decCount - dec_init) ){//When nc=0 the display ended.
  176. char tmpbuff[displayWidth+1];
  177. sprintf(buff,"(%ld)", decCount - dec_init);
  178. sprintf(tmpbuff, "%*s%s",rest ," ", buff);
  179. cCount += putbuff(tmpbuff, os);
  180. // cCount += fprintf(out, "%*s%s",rest," ", buff);
  181. }
  182. cCount += putcr(crlf, os);
  183. sd.ioCount = cCount;
  184. return os;
  185. }

sdostrm.cpp : last modifiled at 2017/08/21 12:07:19(6,755 bytes)
created at 2017/10/07 10:21:14
The creation time of this html file is 2017/10/07 10:30:03 (Sat Oct 07 10:30:03 2017).